import Vue from 'vue'
import Router from 'vue-router'
import MuseUi from 'muse-ui'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import '@/common/css/style.css'
import 'swiper/dist/css/swiper.css'
import 'muse-ui/dist/muse-ui.css'
import 'muse-ui/dist/theme-light.css'
// import music from '@/components/music/music'
// import user from '@/components/user/user'
Vue.use(MuseUi)
Vue.use(VueAwesomeSwiper)
Vue.use(Router)

const Recommend = (resolve) => {
  import('@/components/recommend/recommend').then((module) => {
    resolve(module)
  })
}

const SongList = (resolve) => {
  import('@/components/songList/songList').then((module) => {
    resolve(module)
  })
}

const LeaderBoards = (resolve) => {
  import('@/components/LeaderBoards/LeaderBoards').then((module) => {
    resolve(module)
  })
}

const RankingDetails = (resolve) => {
  import('@/components/RankingDetails/RankingDetails').then((module) => {
    resolve(module)
  })
}

const RecommendList = (resolve) => {
  import('@/components/RecommendList/RecommendList').then((module) => {
    resolve(module)
  })
}

const SongListDetail = (resolve) => {
  import('@/components/SongListDetail/SongListDetail').then((module) => {
    resolve(module)
  })
}

const routes = [
  {
    path: '',
    redirect: '/recommend'
  },
  {
    path: '/recommend',
    component: Recommend,
    meta: {title: '推荐'},
    children: [
      {
        path: ':id',
        component: RecommendList,
        meta: {title: '列表'}
      }
    ]
  },
  {
    path: '/songList',
    component: SongList,
    meta: {title: '歌单'},
    children: [
      {
        path: ':id',
        component: SongListDetail,
        meta: {title: '列表'}
      }
    ]
  },
  {
    path: '/leaderBoards',
    component: LeaderBoards,
    meta: {title: '排行榜'},
    children: [
      {
        path: ':id',
        component: RankingDetails
      }
    ]
  }
]
const router = new Router({
  base: '/music/',
  mode: 'history',
  linkActiveClass: 'active',
  scrollBehavior: (to, from, savedPosition) => {
    console.log(from.matched[1] && from.matched[1].path === '/recommend/:id')
    if (savedPosition) {
      return savedPosition
    } else {
      if (from.matched[1] && from.matched[1].path === '/recommend/:id') {
        from.meta.savedPosition = 0
      } else if (from.meta.keepAlive) {
        from.meta.savedPosition = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
      }
      return {x: 0, y: to.meta.savedPosition || 0}
    }
  },
  routes: routes
})
router.beforeEach((to, from, next) => {
  window.document.title = to.meta.title
  next()
})
export default router
